﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.Domain;
using MyVoteMVC.BackstageManagement.Controllers;
using System.IO;

namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class ProjectManagement : Form, IProjectManagementView
    {
        IProjectManagementController controller;
        public ProjectManagement()
        {
            InitializeComponent();
            controller = new ProjectManagementController(this);

        }

        #region IProjectManagementView Members

        public void GetProjectsComplete(IList<VT_Projects> projects)
        {
            gvProject.DataSource = projects;
        }

        public void CreateProjectComplete(string message)
        {
            MessageBox.Show(message);
            Init();
        }

        public void EditProjectComplete(string message)
        {
            MessageBox.Show(message);
            Init();
        }

        public void DeleteProjectComplete(string message)
        {
            MessageBox.Show(message);
            Init();
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }

        public void GetProjectComplete(VT_Projects project)
        {
            txtAddress.Text = project.Project_Address;
            txtAwards.Text = project.Project_Awards;
            txtDescription.Text = project.Project_Description;
            txtName.Text = project.Project_Name;
            txtType.Text = project.Project_Type;
            txtYear.Text = project.Project_Year;

            if (project.ProjectImage != null && project.ProjectImage.Project_Image != null)
                picProjectImage.Image = GetImage(project.ProjectImage.Project_Image);
        }

        public void GetProjectsByTypeComplete(IList<VT_Projects> projects)
        {
            gvProject.DataSource = projects;
        }

        public void GetProjectsByYearComplete(IList<VT_Projects> projects)
        {
            gvProject.DataSource = projects;
        }

        public void GetProjectTypesComplete(IList<string> types)
        {
            types.Insert(0, "全部");
            cboType.DataSource = types;
        }

        public void GetProjectsYearComplete(IList<string> years)
        {
            years.Insert(0, "全部");
            cboYear.DataSource = years.Where(o => o != null && o != "").ToList();
        }

        #endregion

        private void gvProject_SelectionChanged(object sender, EventArgs e)
        {
            if (gvProject.SelectedRows.Count > 0 && gvProject.SelectedRows[0].DataBoundItem != null)
            {
                VT_Projects project = gvProject.SelectedRows[0].DataBoundItem as VT_Projects;
                controller.GetProject(project.Project_ID);
            }
        }

        private void btnSearchByYear_Click(object sender, EventArgs e)
        {
            string year = "", type = "", kw = txtKeyWord.Text;
            if (cboYear.SelectedItem != null && cboYear.SelectedItem.ToString() != "全部")
            {
                year = cboYear.SelectedItem.ToString();
            }

            if (cboType.SelectedItem != null && cboType.SelectedItem.ToString() != "全部")
            {
                type = cboType.SelectedItem.ToString();
            }

            controller.GetProjects(type, year, kw);
        }

        private void btnSearchByType_Click(object sender, EventArgs e)
        {
            if (cboType.SelectedItem != null)
            {
                controller.GetProjectsByType(cboType.SelectedItem.ToString());
            }
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG文件(*.jpg)|*.jpg|PNG文件(*.png)|*.png|GIF文件(*.gif)|*.gif";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                txtProjectImage.Text = filePath;
                picProjectImage.Image = GetImage(ReadFile(filePath));
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            controller.CreateProject(
                txtYear.Text,
                txtAddress.Text,
                txtName.Text,
                txtType.Text,
                txtAwards.Text,
                txtDescription.Text,
                ReadFile(txtProjectImage.Text)
                );
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
             if (gvProject.SelectedRows.Count > 0 && gvProject.SelectedRows[0].DataBoundItem != null)
            {
                VT_Projects project = gvProject.SelectedRows[0].DataBoundItem as VT_Projects;
                controller.EditProject(
                    project.Project_ID,
                    txtYear.Text,
                    txtAddress.Text,
                    txtName.Text,
                    txtType.Text,
                    txtAwards.Text,
                    txtDescription.Text,
                   (string.IsNullOrWhiteSpace(txtProjectImage.Text) ? null :  ReadFile(txtProjectImage.Text))
                    );
            }
            else
            {
                MessageBox.Show("请选择要更新的项目");
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProjectManagementView_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void ProjectManagementView_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private  byte[] ImageDatabytes(Bitmap myBitmap)
        {
            try
            {
                using (MemoryStream curImageStream = new MemoryStream())
                {
                    myBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Png);
                    curImageStream.Flush();
                    byte[] bmpBytes = curImageStream.ToArray();
                    return bmpBytes;
                }
            }
            catch
            {
                return null;
            }
        }

        private  Bitmap GetImage(byte[] ImageDatas)
        {
            try
            {
                using (MemoryStream ImageMS = new MemoryStream())
                {
                    ImageMS.Write(ImageDatas, 0, ImageDatas.Length);
                    Bitmap resultBitmap = new Bitmap(ImageMS);
                    return resultBitmap;
                }
            }
            catch
            {
                return null;
            }
        }

        private byte[] ReadFile(string fileName)
        {
            FileStream pFileStream = null;
            byte[] pReadByte = null;

            try
            {
                pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
        }

        private void Init()
        {
            controller.GetProjects();
            controller.GetProjectsYear();
            controller.GetProjectTypes();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (gvProject.SelectedRows.Count > 0 && gvProject.SelectedRows[0].DataBoundItem != null)
            {
                VT_Projects project = gvProject.SelectedRows[0].DataBoundItem as VT_Projects;
                controller.DeleteProject(project.Project_ID);
            }
            else
            {
                MessageBox.Show("请选择要删除的项目");
                return;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void btnDelList_Click(object sender, EventArgs e)
        {
            controller.DeleteProject(gvProject.DataSource as List<VT_Projects>);

        }
    }
}
